const { readFileSync, writeFileSync } = require('fs');
const { resolve } = require('path');
require('./Function.js');

function readFile(path) {
  return readFileSync(resolve(__dirname, path), 'utf8');
}

function writeFile(path, data) {
  writeFileSync(resolve(__dirname, path), JSON.stringify(data));
}

// 文件操作 AOP
function fileOperation(path, fn) {
  return fn.before(() => {
    // 把结果传给fn执行 
    return JSON.parse(readFile(path) || '[]');
  }).after((data) => {
    writeFile(path, data);
    return data
  })();
}

module.exports = {
  fileOperation
}